<?php

namespace app\admin\controller\marketing;

use app\common\controller\AdminController;
use app\services\marketing\coupon\CouponService;
use think\App;

/**
 * Class Coupon
 * @package app\admin\controller\marketing
 */
class Coupon extends AdminController
{
    /**
     * @var CouponService
     */
    private $service;

    public function __construct(App $app, CouponService $service)
    {
        parent::__construct($app);

        $this->service = $service;

        $this->assign('getCouponTypeList', $this->service->getCouponTypeList());
        $this->assign('getUseTypeList', $this->service->getUseTypeList());
        $this->assign('getReceiveTypeList', $this->service->getReceiveTypeList());
        $this->assign('getIsTimeLimitList', $this->service->getIsTimeLimitList());
        $this->assign('getIsPermanentList', $this->service->getIsPermanentList());
        $this->assign('getStatusList', $this->service->getStatusList());
    }

    /**
     * Description: 获取首页列表
     *
     * @return mixed|\think\response\Json
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\db\exception\DataNotFoundException
     */
    public function index()
    {
        if ($this->request->isAjax()) {
            return layuiTabSuc($this->service->getList($this->request->get()));
        }
        return $this->fetch();
    }

    /**
     * Description: 添加数据
     *
     * @return mixed
     */
    public function add()
    {
        if ($this->request->isAjax()) {
            $data = $this->request->post();
            if ($data['is_time_limit'] === 0) {
                if ((strtotime($data['end_time']) - strtotime($data['start_time'])) < 0) {
                    $this->error('结束时间不能小于开始时间');
                }
            }
            $this->service->save($data);
            $this->success('保存成功');
        }
        return $this->fetch();
    }

    /**
     * Description: 编辑数据
     *
     * @param int $id
     *
     * @return mixed
     */
    public function edit(int $id)
    {
        $row = $this->service->get($id);
        if ($row->isEmpty()) {
            $this->error('数据不存在');
        }

        if ($this->request->isPost()) {
            $data = $this->request->post();
            if ($data['is_time_limit'] === 0) {
                if ((strtotime($data['end_time']) - strtotime($data['start_time'])) < 0) {
                    $this->error('结束时间不能小于开始时间');
                }
            }
            $row->save($data);
            $this->success('保存成功');
        }
        $this->assign('row', $row);
        return $this->fetch();
    }

    /**
     * Description: 删除数据
     *
     * @param array|int $id 删除id
     */
    public function delete($id)
    {
        $this->service->delete($id);
        $this->success('删除成功');
    }

    /**
     * Description: 导出数据
     *
     * @throws \PhpOffice\PhpSpreadsheet\Exception
     * @throws \PhpOffice\PhpSpreadsheet\Writer\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function export()
    {
        $this->service->exportData($this->request->get());
    }

    /**
     * Description: 更新指定字段
     *
     * @param int    $id
     * @param string $field
     * @param string $value
     */
    public function setField(int $id, string $field, string $value)
    {
        $this->service->update($id, [$field => $value]);
        $this->success('修改成功！');
    }
}